#!/usr/bin/env python
# -*- coding: utf-8 -*-
# @Time    : 2021-04-20 23:41
# @Author  : zeshan
# @File    : base_page.py
import yaml

from selenium import webdriver
from webdriver_manager.chrome import ChromeDriverManager
from selenium.webdriver.support.wait import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
from comm import config
from comm.logger import Logger
from utils.do_yaml import DoYaml

cfg = config.COMMCFG
logger = Logger("base_page.py").getLog()
wait_time = 10

class BasePage:
    def __init__(self, driver=None):
        if driver == None:
            # self.driver = webdriver.Chrome()
            # selenium 3
            self.driver = webdriver.Chrome(ChromeDriverManager().install()) #自动下载webdriver
            self.driver.get(url=cfg.LOGIN_URL)
            cookies = DoYaml().read_yaml(file_path=cfg.COOKIES_PATH)
            for cookie in cookies:
                self.driver.add_cookie(cookie)
            self.driver.get(url=cfg.MAIN_URL)
        else:
            self.driver = driver

        self.driver.maximize_window()
        self.driver.implicitly_wait(wait_time)

    def find_element(self, by, ele=None):
        try:
            if ele == None:
                WebDriverWait(self.driver, wait_time).until(EC.presence_of_element_located(by))
                ele_obj = self.driver.find_element(*by)
            else:
                WebDriverWait(self.driver, wait_time).until(EC.presence_of_element_located((by, ele)))
                ele_obj = self.driver.find_element(by, ele)
            logger.info("find_element (%s,%s)" % (by, ele))
        except Exception as e:
            logger.error("Failed to find_element (%s,%s) :%s" % (by, ele, e))
        else:
            return ele_obj

    def find_elements(self, by, ele=None):
        try:
            if ele == None:
                WebDriverWait(self.driver, wait_time).until(EC.presence_of_element_located(by))
                ele_obj = self.driver.find_elements(*by)
            else:
                WebDriverWait(self.driver, wait_time).until(EC.presence_of_element_located((by, ele)))
                ele_obj = self.driver.find_elements(by, ele)
            logger.info("find_elements (%s,%s)" % (by, ele))
        except Exception as e:
            logger.error("Failed to find_elements (%s,%s) :%s" % (by, ele, e))
        else:
            return ele_obj

    def wait_ele_visibility(self,by, ele=None):
        try:
            if ele == None:
                WebDriverWait(self.driver, wait_time).until(EC.visibility_of_element_located(by))
            else:
                WebDriverWait(self.driver, wait_time).until(EC.visibility_of_element_located((by,ele)))
            logger.info("visibility_of_element_located element:(%s,%s)" % (by,ele))
        except Exception as e:
            logger.info("visibility_of_element_located failed :%s" % e)

    def execute_script(self, js):
        try:
            self.driver.execute_script(js)
            logger.info("execute_script suecess:%s" % js)
        except Exception as e:
            logger.error("Failed to execute_script:%s" % js)
